package lib.base;

import java.util.ArrayList;
import java.util.Calendar;

import com.google.gdata.data.DateTime;

import lib.data.Employee;

public class EgoHoliday
{
	public static String LENGTH_FULL = "";
	public static String LENGTH_HALF = "half";
	
	public static String STATUS_REQUESTED = "requested";
	public static String STATUS_ACCEPTED = "";
	public static String STATUS_DENIED = "denied";
	
	public static String TYPE_NORMAL = "normal";
	public static String TYPE_SPECIAL = "special";
	
	private String status;
	private String type;
	private boolean halfDay;
	private Employee employee;
	
	private String firstDay;
	private String lastDay;
	
	public EgoHoliday(Employee emp)
	{
		status = STATUS_REQUESTED;
		type = TYPE_NORMAL;
		halfDay = false;
		
		firstDay = "";
		lastDay = "";
		
		setEmployee(emp);
	}
	
	public String getStatus()
	{
		return new String(status);
	}
	
	public void setStatus(String s)
	{
		status = s;
	}
	
	public String getType()
	{
		return new String(type);
	}
	
	public void setType(String s)
	{
		type = s;
	}
	
	/**
	 * returns the earliest stamp
	 * @return
	 */
	public String getStartStamp()
	{
		return new String(firstDay);
	}
	
	/**
	 * returns the latest stamp
	 * @return
	 */
	public String getEndStamp()
	{
		return new String(lastDay);
	}
	
	public void setStartStamp(String s)
	{
		firstDay = s;
	}
	
	public void setEndStamp(String s)
	{
		lastDay = s;
	}
	
	public String toString()
	{
		return "EgoHoliday["+firstDay+" / "+lastDay+"]";
	}
	
	/**
	 * returns the length of the holiday
	 * @return
	 */
	public float getLength()
	{
		if (getDays().size() == 1 && halfDay)
		{
			return (float) 0.5;
		}
		return getDays().size();
	}
	
	/**
	 * returns a arraylist with stamps of every
	 * day of this holiday
	 * @return
	 */
	public ArrayList<String> getDays()
	{
		ArrayList<String> days = new ArrayList<String>();
		
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis( DateTime.parseDate(getStartStamp()).getValue() );

		// get publicHolidays
		ArrayList<String> publicHolidays = null;
		if (employee != null)
		{
			try {
				publicHolidays = employee.getCustomer().holidayManager.getPublicHolidays(
					employee
					, String.valueOf(cal.get(Calendar.YEAR))
				);
			} catch (Exception e) {
			}
		}

		while (cal.getTimeInMillis() <= DateTime.parseDate(getEndStamp()).getValue())
		{
			// start + end date
			String s = Integer.toString(cal.get(Calendar.YEAR)) + "-"
						+ (cal.get(Calendar.MONTH)<9 ? "0": "") + Integer.toString(cal.get(Calendar.MONTH)+1) + "-"
						+ (cal.get(Calendar.DAY_OF_MONTH)<10 ? "0": "") + Integer.toString(cal.get(Calendar.DAY_OF_MONTH));
			
			if (employee != null)
			{
				// check if it is a workingDay
				int weekDay = cal.get(Calendar.DAY_OF_WEEK) -2;
				if (weekDay == -1)
				{ // sunday
					weekDay = 6;
				}
				weekDay = (int) Math.pow(2, weekDay);
				if ((Integer.valueOf( employee.getField("workingDays").toString() ) & weekDay) == weekDay)
				{
					// ... and check if there's a public holiday
					boolean pubhol = false;
					if (publicHolidays != null)	for (int i=0; i<publicHolidays.size(); i++)
					{
						if (publicHolidays.get(i).equals( s ))
						{
							// it's a public holiday
							pubhol = true;
							break;
						}
					}
					if (!pubhol)
					{
						days.add(s);
					}
				}
			} else
			{
				days.add(s);
			}
			
			cal.add(Calendar.DAY_OF_YEAR, 1); // next day
		}

		return days;
	}
	
	/**
	 * set corresponding Employee
	 * @param e Employee
	 */
	public void setEmployee(Employee e)
	{
		employee = e;
	}
	
	public Employee getEmployee()
	{
		return employee;
	}
	
	public void setHalfDay(boolean t)
	{
		halfDay = t;
	}
	
	public boolean isHalfDay()
	{
		return halfDay;
	}
}
